package model;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;

public class Project {
	private ArrayList<Host> hosts;
	private ArrayList<File> files;
	
	
	public Project(){
		hosts = new ArrayList<Host>();
		files = new ArrayList<File>();
	}
	public void addHost(Host host){
		int i = 0;
		while (i<hosts.size()){
			if (hosts.get(i).isIPLessThan(host.getIPAddress())){
				
			}
			i++;
		}
		hosts.add(i, host);
	}
	public void addFile(File file){
		files.add(file);
	}
	public void setFiles(File[] files){
		this.files = new ArrayList<File>();
		for (File file: files){
			this.files.add(file);
		}
	}
	public Scan addHost(String iPAddress, String command) {
		//if the host indicated by the IP Address does not exist in the project, add the host and return it
		//otherwise, if the host does exist in the project return that host
				
		Scan newScan = new Scan(command, iPAddress);
		for (Host host: hosts){
			if (host.getIPAddress().equals(iPAddress)){
				host.addScan(newScan);
				return newScan;
			}
		}
		
		Host host = new Host(iPAddress);
		hosts.add(host);
		host.addScan(newScan);
		return newScan;
		
	}
	public void checkForInterestingPorts() {
		//checks for a specific list of interesting ports
		checkForPorts();
	}
	public void checkForPorts(){
		
	}
	public Host[] getHosts() {
		return hosts.toArray(new Host[0]);
	}
	public void printHostPorts(){
		PortEntry[] hostPorts;
		for (Host host: hosts){
			hostPorts = host.getUniquePorts();
			System.out.println(host.getIPAddress());
			for (PortEntry hostPort: hostPorts){
				System.out.println("\t"+hostPort.getPortNumber()+"\t"+hostPort.getService());
			}
		}
	}
	public LinkedList<LinkedList<String>> getProjectInfo() {
		
		LinkedList<LinkedList<String>>projectInfo = new LinkedList<LinkedList<String>>();
		
		Host[] hosts = getHosts();
		for (Host host : hosts){
			projectInfo.add(host.getHostInfo());
		}
		return projectInfo;
	}
	public DefaultMutableTreeNode getTree(){
		DefaultMutableTreeNode tree = new DefaultMutableTreeNode("Project");
		for (Host host : hosts){
			tree.add(host.getTreeNode());
		}
		return tree;
	}
	
}
